//app.js
App({
    onLaunch: function() {

        let that = this;
        // 展示本地存储能力
        var logs = wx.getStorageSync('logs') || []
        logs.unshift(Date.now())
        wx.setStorageSync('logs', logs)

        // 登录
        wx.login({
                success: res => {
                    // 发送 res.code 到后台换取 openId, sessionKey, unionId
                }
            })
            /* 
            https://apis.map.qq.com/ws/place/v1/search?
            keyword=%E9%85%92%E5%BA%97
            &boundary=nearby(43.79547,87.63356,5000)
            &key=6FUBZ-J7EHD-7Z34C-P77Q2-FDXHF-BPBFJ*/
            // 获取用户信息
        wx.getStorage({
            key: 'uinfo',
            success: (result) => {
                wx.getSetting({
                    success: res => {
                        // console.log(res)
                        if (res.authSetting['scope.userInfo']) {
                            // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                            wx.getUserInfo({
                                success: res => {
                                    // console.log(res)
                                    // 可以将 res 发送给后台解码出 unionId
                                    that.globalData.userInfo = res.userInfo
                                    wx.setStorage({
                                        key: "uinfo",
                                        data: {
                                            'userInfo': res.userInfo
                                        }
                                    });
                                    // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                                    // 所以此处加入 callback 以防止这种情况
                                    if (that.userInfoReadyCallback) {
                                        that.userInfoReadyCallback(res)
                                    }
                                }
                            })
                        }
                    }
                })
            },
            fail: () => {},
            complete: () => {}
        });


        wx.getLocation({
            type: "wgs84",
            success: function(res) {
                var latitude = res.latitude;
                var longitude = res.longitude;
                wx.setStorage({
                    key: "locations",
                    data: {
                        'latitude': latitude,
                        'longitude': longitude
                    }
                });
                console.log(res, "appjs 获取一次位置");
                that.globalData.location = {
                    latitude,
                    longitude
                }
            }
        })

        that.getMachineInfo()
            // 去除分享阿按钮

    },
    getMachineInfo() {
        let that = this
        wx.getSystemInfo({
            success: function(res) {
                console.log(res)
                console.log("------app获取高度", res.statusBarHeight)
                let statusBarHeight = res.statusBarHeight
                let sharebtnTop = wx.getMenuButtonBoundingClientRect().top
                let navHeight = wx.getMenuButtonBoundingClientRect().height + (sharebtnTop - statusBarHeight) * 2
                that.globalData.statusBarHeight = statusBarHeight
                that.globalData.navHeight = navHeight
            }
        })

        console.log(wx.getMenuButtonBoundingClientRect())

    },
    globalData: {
        userInfo: null,
        mapKey: '6FUBZ-J7EHD-7Z34C-P77Q2-FDXHF-BPBFJ'
    },
})